package com.zhn.evsi.controller.Evsi;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zhn.evsi.common.Result;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketGroupsHistoryDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketGroupsOrdersDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketStatDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.domain.entity.evsi.EvsiMarketgroups;
import com.zhn.evsi.exception.CustomException;
import com.zhn.evsi.service.IEvsiMarketgroupsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "商品组管理")
@RestController
@RequestMapping("evsi/marketgroups")
public class EvsiMarketgroupsController {

    @Value("${eve.characterId}")
    private Long characterId; // TODO 上线后删除

    @Value("${eve.jitaRegionLimit}")
    private String regionLimit;

    @Autowired
    private IEvsiMarketgroupsService marketgroupsService;

    /**
     * 查询我的订单
     * 并查询到我的订单的市场情况
     * */
    @ApiOperation("查询我的订单")
    @GetMapping("/groupsOrders")
    private Result groupsOrders() {
        return Result.success(marketgroupsService.groupsOrders());
    }

    /**
     * 查询我的订单历史
     * */
    @ApiOperation("查询我的订单历史")
    @GetMapping("/groupsHistory")
    public Result groupsHistroy(@RequestParam Long characters) {
        if (ObjectUtil.isNull(characters)) {
            throw new CustomException("角色id不可为空，请重新授权角色！");
        }
        characters = this.characterId; // TODO 上线后删除
        List<EvsiMarketGroupsHistoryDTO> evsiMarketGroupsHistoryDTOS = marketgroupsService.groupsHistory(characters);

        return Result.success(evsiMarketGroupsHistoryDTOS);
    }

    /**
     * 通过商品组获取商品具体信息
     * */
    @ApiOperation("获取商品信息")
    @GetMapping("/groupsType")
    public Result groupsType(@RequestParam(defaultValue = "") Integer marketGroupId) {
        List<EvsiInvtypes> evsiInvtypes = marketgroupsService.groupsType(marketGroupId);
        return Result.success(evsiInvtypes);
    }

    /**
     * 通过物品id查询市场详情
     * */
    @ApiOperation("查询市场详情")
    @GetMapping("/marketStat")
    public Result marketStat(@RequestParam(defaultValue = "") Integer[] typeId,
                             @RequestParam(defaultValue = "") String regionLimit) {
        regionLimit = this.regionLimit;
        List<EvsiMarketStatDTO> evsiMarketStatDTOS = marketgroupsService.marketStat(typeId, regionLimit);
        return Result.success(evsiMarketStatDTOS);
    }

    /**
     * 构造市场商品组树状结构
     *
     * @param state - (0:全部商品组；
     *              1：；
     *              2：；
     *              3：；
     *              4：舰船商品；)
     * */
    @ApiOperation("查询商品组树状结构")
    @GetMapping("/groupsMenu")
    public Result groupsMenu() {
        List<EvsiMarketgroups> evsiMarketgroups = marketgroupsService.groupsMenu(0);
        return Result.success(evsiMarketgroups);
    }

    /**
     * 更新商品信息
     * */
    @ApiOperation("更新商品信息")
    @GetMapping("/updateGroups")
    public Result updateGroups() {
        /**
         * 从官网下载yaml文档后将yaml存入表中
         * */
        return Result.success();
    }

    /**
     * 估价
     * */
    @ApiOperation("估价")
    @PostMapping("/valuation")
    public Result valuation(@RequestBody String text) {
        String valuation = marketgroupsService.valuation(text);
        return Result.success(valuation);
    }

    /**
     * 估价回调
     * */
    @ApiOperation("估价回调")
    @GetMapping("/valuation/{randomString}")
    public Result valuationCallBack(@PathVariable String randomString) {
        if (StrUtil.isNotEmpty(randomString)) {
            Map<String, Object> map = marketgroupsService.valuationCallBack(randomString);
            return Result.success(map);
        }
        return Result.success();
    }

    /**
     * 市场倒卖
     * */
}
